/**
 * Created by caozengling on 2017/9/30.
 */
var uploadfiles = new Array(); // 要上传的文件列表路径
var form;
var index_bill=1;//计算多个 收入支出 账单
var index_c=0;//计算多个 停车费
var element;
var form;
var type_cards = QueryUtils.GetQueryString("type");
layui.use(['jquery', 'element', 'layer', 'laydate', 'form','upload'], function () {
    $ = layui.jquery, element = layui.element, layer = layui.layer, laydate = layui.laydate, form = layui.form;
    var upload = layui.upload;
    laydate.render({
        elem: '#START_DATE'
        , format: 'yyyy-MM-dd'
    });
    laydate.render({
        elem: '#END_DATE'
        , format: 'yyyy-MM-dd'
    });
    //输入金额 自动加小数点
    /*$("input").on("blur",function(){
        $(this).val(parseFloat($(this).val()).toFixed(2));
    });*/

    //取消
    $(".cancel").click(function () {

    });
    //开始日期
    laydate.render({
        elem: '#start'
        ,format: 'yyyy-MM-dd'
    });
    //结束日期
    laydate.render({
        elem: '#end'
        ,format: 'yyyy-MM-dd'
    });
    //应付款时间
    laydate.render({
        elem: '#pay_date'
        ,format: 'yyyy-MM-dd'
    });
    //费用信息多选 激活事件
    form.on('checkbox', function(data){
       if("租金"==data.value){

       }
    });

    $.ajax({
        url: "/property/propertyListALL/",
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            if (res.errcode === "0") {
                for(var i=0;i<res.data.length;i++){
                    //console.log(res.data);
                    $(".zdlx").append("<div class=\"layui-col-xs2\">\n" +
                        "                                <input type=\"checkbox\" name=\""+res.data[i].CODE+"\" lay-filter=\"addbills\" value=\""+res.data[i].CODE+"\" lay-skin=\"primary\" title=\""+res.data[i].CODE+"\">\n" +
                        "                            </div>");
                }
            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });


    if("sell_card"==type_cards){
        var  val="卖卡";
        str='<div class="extra-content'+val+'-content"data-id="' + val + '">\n' +
            '<fieldset class="layui-elem-field">\n' +
            '                            <legend>'+val+'</legend>\n' +
            '                            <div class="layui-field-box">\n' +
            '                                <div class="layui-col-xs1">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '停车费'+
            '                                    </div>\n' +
            '                                </div>\n' +
            '                              <div class="layui-col-xs2">\n' +
            '                                    <div class="layui-input-inline room-select2">\n' +
            '                                        <input type="text" name="room1" readonly placeholder="请选择房源项目"\n' +
            '                                               class="layui-input choose-room1" onclick="operate.roomClick(this,' +1+');">\n' +
            '                                        <input type="hidden" name="ROOMID1" placeholder=""\n' +
            '                                               class="layui-input ROOMID1" >\n' +
            '                                        <div>\n' +
            '                                            <div id="room-select-box-container" class="room-select-box-container room-select-box-container1">\n' +
            '                                                <div class="displayflex">\n' +
            '                                                    <dl class="box-container business-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container building-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container unit-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container floor-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container room-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                </div>\n' +
            '                                            </div>\n' +
            '                                        </div>\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                           <div class="MKXFQH1">\n' +
            '                                <div class="layui-col-xs3">\n' +
            '                                    <input type="number" class="layui-input" name="ROOM_MONEY1" id="test5" placeholder="">\n' +
            '                                    <span class="label-money">元</span>\n' +
            '                                </div>\n' +

            '                                 <div class="layui-col-xs1">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '                                        <input type="number" class="layui-input" name="PARK_CARD1" id="PARK_CARD" placeholder="">\n' +
            '                                        <span class="label-money">张</span>\n' +
            '                                    </div>\n' +
            '                                </div>'+
            '                                <div class="layui-col-xs2"  style="width: 23%">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '                                        <select name="ALLOT_BILL1" lay-verify="required" lay-search="">\n' +
            '                                            <option value="0" selected="">公户</option>\n' +
            '                                            <option value="1">私户</option>\n' +
            '                                        </select>\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                             </div>\n' +
            '                                <div class="bill-add">\n' +
            '                                <span onclick="operate.addTCF(this,\''+val+'\')">\n' +
            '                                        <i class="fa fa-plus-circle" aria-hidden="true"></i>\n'+
            '                                        <span>添加</span>\n' +
            '                                    </span>\n' +
            '                                </div>\n' +
            '                            </div>\n' +
            '                            <div class="layui-form-item bill-arr">\n' +
            '                            </div>\n' +
            '                            <br>\n' +
            '                        </fieldset></div>'


        $(".zhangdan-jiben-info").find(".layui-bill").append(str);
        form.render("select");
    }

    //选中 买卡  和消费 事件
    form.on('select(MKXF)', function(data){
        $(this).parent().parent().find((".layui-field-box"))
        //alert($(this).parent().parent().find((".layui-field-box")))
        //alert(data.value)
        //$(".MKXFQH").html('');
        //alert(index_c)
        if("2"==data.value){
            $(this).parent().parent().parent().parent().parent().find(".MKXFQH" +index_c+ "").html('<div class="layui-col-xs3">\n' +
                '                                    <input type="number" class="layui-input" name="" id="test5" placeholder="">\n' +
                '                                    <span class="label-money">元</span>\n' +
                '                                </div>\n' +
                '\n' +
                '                                <div class="layui-col-xs2">\n' +
                '                                     <div class="layui-input-inline room-select2">\n' +
                '                                        <input type="text" name="room1" readonly placeholder="请选择房源项目"\n' +
                '                                               class="layui-input choose-room'+index_c+'" onclick="operate.roomClick(this,' +index_c+');">\n' +
                '                                        <div>\n' +
                '                                        <input type="hidden" name="ROOMID'+index_c+'" placeholder=""\n' +
                '                                               class="layui-input ROOMID'+index_c+'" >\n' +
                '                                           <div id="room-select-box-container" class="room-select-box-container room-select-box-container'+index_c+'">\n' +
                '                                                <div class="displayflex">\n' +
                '                                                    <dl class="box-container business-box-container">\n' +
                '                                                    </dl>\n' +
                '                                                    <dl class="box-container building-box-container">\n' +
                '                                                    </dl>\n' +
                '                                                    <dl class="box-container unit-box-container">\n' +
                '                                                    </dl>\n' +
                '                                                    <dl class="box-container floor-box-container">\n' +
                '                                                    </dl>\n' +
                '                                                    <dl class="box-container room-box-container">\n' +
                '                                                    </dl>\n' +
                '                                                </div>\n' +
                '                                            </div>\n' +
                '                                        </div>\n' +
                '                                    </div>\n' +
                '                                </div>\n' +
                '                                <div class="layui-col-xs1">\n' +
                '                                    <div class="">\n' +
                '                                        <input type="number" class="layui-input" name="PARK_CARD" id="PARK_CARD" placeholder="">\n' +
                '                                        <span class="label-money">张</span>\n' +
                '                                    </div>\n' +
                '                                </div>'+
                '                                <div class="layui-col-xs2">\n' +
                '                                    <div class="layui-input-inline">\n' +
                '                                        <select name="ALLOT_BILL'+index_c+'" lay-verify="required" lay-search="">\n' +
                '                                            <option value="0" selected="">公户</option>\n' +
                '                                            <option value="1">私户</option>\n' +
                '                                        </select>\n' +
                '                                    </div>\n' +
                '                                </div>\n');

        }

        laydate.render({
            elem: '#PART_START'
            , type: 'datetime'
        });
        laydate.render({
            elem: '#PART_END'
            , type: 'datetime'
        });
        form.render();
    });
    //消费类型 消费卡 消费 元
    form.on('select(XFLX)', function(data){
        $(".xflx").html();
        if("1"==data.value){
            $(this).parent().parent().parent().parent().find($(".room-select2")).show();
            $(".xflx").html("<div class=\"layui-col-xs1\">\n" +
                "                                        <input type=\"number\" class=\"layui-input\" name=\"PARK_CARD\" id=\"PARK_CARD\" placeholder=\"\">\n" +
                "                                        <span class=\"label-money\">张</span>\n" +
                "                                </div>");
        }
        if("2"==data.value){
            $(this).parent().parent().parent().parent().find($(".room-select2")).hide();
            $(".xflx").html(" <div class=\"layui-col-xs1\">\n" +
                "                                        <input type=\"number\" class=\"layui-input\" name=\"TOTAL_PRICE\" id=\"TOTAL_PRICE\" placeholder=\"\">\n" +
                "                                        <span class=\"label-money\">元</span>\n" +
                "                                </div>"+
                '                                <div class="layui-col-xs1">\n' +
                '                                        <select name="ALLOT_BIL" lay-verify="required" lay-search="">\n' +
                '                                            <option value="0" selected="">公户</option>\n' +
                '                                            <option value="1">私户</option>\n' +
                '                                        </select>\n' +
                '                                </div>\n');
        }
        form.render();
    });
    //监听提交
    form.on('submit(billadd)', function(data){
        debugger
        var str="ALLOT_BILL,PARK_CARD,ROOMID,ROOM_MONEY";
       var PARK_ID = QueryUtils.GetQueryString('PARK_ID');
        debugger
        var data1=data.field;
        var jsonList=[];
        for (var i=1;i<100;i++) {
            var jsonData = {};
            var ALLOT_BILL = "ALLOT_BILL" + [i];
            var PARK_CARD = "PARK_CARD" + [i];
            var ROOMID = "ROOMID" + [i];
            var ROOM_MONEY = "ROOM_MONEY" + [i];
            if (data1[ALLOT_BILL]) {
                jsonData.ALLOT_BILL=data1[ALLOT_BILL];
                jsonData.PARK_CARD=data1[PARK_CARD];
                jsonData.ROOMID=data1[ROOMID];
                jsonData.ROOM_MONEY=data1[ROOM_MONEY];
                jsonData.INFO=data1.INFO;
                jsonData.PRICE_TYPE="2";
                jsonData.PARK_ID=PARK_ID;
                jsonList.push(jsonData);

            }else{
                continue;
            }
        }



        data1.ATTACHMENT = uploadfiles;


        var data = {};
        var sendData = {};
        sendData.DATA = JSON.stringify(jsonList);

        var uploadData = {};
        uploadData.DATA = JSON.stringify(uploadfiles);

        layer.msg('确认添加账单吗', {
            time: 0
            , btn: ['确定', '取消']
            , shade: 0.3
            , yes: function (index) {
                $.ajax({
                    url: "/billController/saveTCar",
                    data: {sendData:sendData,uploadData:uploadData},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        debugger
                        if (res.errcode === "0") {
                            var i = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(i);  // 关闭修改房间页面
                            parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出修改成功
                            window.parent.reloadTable(null);
                           /* window.parent.frames[0].reloadtable();
                            window.parent.frames[0].reloadszTbale();
                            window.parent.frames[0].reloadyfTbale();
                            window.parent.frames[0].reloadyclTbale();
                            window.parent.frames[0].reloadzclbTbale();*/
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }


                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });

            }
        });


        return false;
    });

    //多文件列表示例
    var files;
    var demoListView = $('#demoList')
        , uploadListIns = upload.render({
        elem: '#testList'
        , url: '/servlet/UploadHandleServlet'
        // ,accept: 'file'
        , multiple: true
        , auto: false
        , bindAction: '#testListAction'
        , choose: function (obj) {
            files = obj.pushFile(); //将每次选择的文件追加到文件队列
            //读取本地文件
            obj.preview(function (index, file, result) {
                var tr = $(['<tr id="upload-' + index + '">'
                    , '<td><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img"></td>'
                    , '<td>' + file.name + '</td>'
                    , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                    , '<td>等待上传</td>'
                    , '<td>'
                    , '<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                    , '<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                    , '</td>'
                    , '</tr>'].join(''));

                //单个重传
                tr.find('.demo-reload').on('click', function () {
                    obj.upload(index, file);
                    return false;
                });

                //删除
                tr.find('.demo-delete').on('click', function () {
                    delete files[index]; //删除对应的文件
                    tr.remove();
                });

                demoListView.append(tr);
            });
        }
        , done: function (res, index, upload) {
            if (res.errcode == 0) { //上传成功
                var obj = {};
                obj.name = res.data.fileName;
                obj.ext = res.data.extName;
                obj.url = res.data.fileUrl;
                uploadfiles.push(obj);
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(3).html('<span style="color: #5FB878;">上传成功</span>');
                tds.eq(4).html(''); //清空操作
                delete files[index]; //删除文件队列已经上传成功的文件
                return;
            }
            this.error(index, upload);
        }
        , error: function (index, upload) {
            var tr = demoListView.find('tr#upload-' + index)
                , tds = tr.children();
            tds.eq(3).html('<span style="color: #FF5722;">上传失败</span>');
            tds.eq(4).find('.demo-reload').removeClass('layui-hide'); //显示重传
        }
    });
    console.log(uploadfiles);


});
var $, element, layer, laydate, form;
var today = DateUtils.dateFormat(new Date(), "yyyy-MM-dd"); // 今天的日期
var uploadfiles = new Array(); // 要上传的文件列表路径
var tips = false;   // 合同信息生成明细修改提示状态
var unit = 0;
var unitHtml = "元/㎡·天";
var ftype = 1;
var wtype = 1;  // 水费选择默认价格还是自定义 0默认 1自定义
var etype = 1;  // 电费选择默认价格还是自定义 0默认 1自定义
var chooseIdArray = new Array(); // 选中的ID数组
var chooseItemArray = new Array(); // 选中的项目数组
var chooseOnOff = false;
/**
 * 通过房间详情点击的新增合同时
 * 获取当前选中的房间ID
 */
var roomId = QueryUtils.GetQueryString("ROOM_ID");
layui.use(['jquery', 'element', 'layer', 'laydate', 'form', 'upload'], function () {
    // 加载元素控件
    $ = layui.jquery, element = layui.element, layer = layui.layer, laydate = layui.laydate, form = layui.form;
    var upload = layui.upload;
    /*------- 下面为房间下拉框相关js -------*/
    $("input[name='room']").click(function (e) {
        chooseOnOff = true;
        $(".room-select-box-container").hide();
        $(".room-select-box-container-btn").hide();
        $(this).parent().find(".room-select-box-container").show();
        if (chooseItemArray.length > 0) {
            $(this).parent().find(".room-select-box-container-btn").show();
        }
        e.stopPropagation();
    });
    $(".room-box-container dd").click(function (e) {
        $(".room-select-box-container").hide();
        $(".room-select-box-container-btn").hide();
    });
    $(document).click(function (event) {
        var e = e || window.event; //浏览器兼容性
        var elem = e.target || e.srcElement;
        while (elem) { //循环判断至跟节点，防止点击的是div子元素
            if (elem.id && elem.id == 'room-select-box-container') {
                return;
            }
            elem = elem.parentNode;
        }
        //operate.chooseRoom($(".room-select-box-container:visible"));//填充下一个行数据
        $(".room-select-box-container").hide();
        $(".room-select-box-container-btn").hide();
        if (chooseOnOff == true) {
            operate.chooseRoom();
            chooseOnOff = false;
        }
    });
    /*------- 上面为房间下拉框相关js -------*/
    // 项目复选框
    form.on('checkbox(cb-item)', function (data) {
        var obj = {};
        obj.id = data.value;
        obj.name = data.othis.parent().find("input").data('name');
        obj.area = data.othis.parent().find("input").data('area');
        if (!!data.othis.parent().find("input").data('wcode') && data.othis.parent().find("input").data('wcode') != undefined && data.othis.parent().find("input").data('wcode') != "undefined") {
            obj.wid = data.othis.parent().find("input").data('wcode');
        } else {
            obj.wid = "";
        }
        if (!!data.othis.parent().find("input").data('ecode') && data.othis.parent().find("input").data('ecode') != undefined && data.othis.parent().find("input").data('ecode') != "undefined") {
            obj.eid = data.othis.parent().find("input").data('ecode');
        } else {
            obj.eid = "";
        }
        var val = data.value;
        var title = data.elem.title;
        if (data.elem.checked == true) {
            console.log("项目：" + val + "选中了");
            chooseIdArray.push(val);
            chooseItemArray.push(obj);
        } else if (data.elem.checked == false) {
            // console.log("项目：" + val + "取消选中了");
            chooseIdArray = $.grep(chooseIdArray, function (value) {
                return value != val;
            });
            for (var j = 0; j < chooseItemArray.length; j++) {
                if (chooseItemArray[j].id == obj.id) {
                    chooseItemArray.splice(j, 1);
                    j--;
                }
            }
        }
        change.itemSelectChange();
    });
    // 楼层复选框
    form.on('checkbox(cb-floor)', function (data) {
        var val = data.value;
        var title = data.elem.title;
        if (data.elem.checked == true) {
            // console.log("楼层：" + val + "选中了");
            chooseIdArray.push(val);
        } else if (data.elem.checked == false) {
            // console.log("楼层：" + val + "取消选中了");
            chooseIdArray = $.grep(chooseIdArray, function (value) {
                return value != val;
            });
            $(".room-box-container").find("input[name='cb-room']").each(function () {
                val = $(this).val();
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == val) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
            });
        }
    });
    // 房间复选框
    form.on('checkbox(cb-room)', function (data) {
        var obj = {};
        obj.id = data.value;
        obj.name = data.othis.parent().find("input").data('name');
        obj.area = data.othis.parent().find("input").data('area');
        if (!!data.othis.parent().find("input").data('wcode') && data.othis.parent().find("input").data('wcode') != undefined && data.othis.parent().find("input").data('wcode') != "undefined") {
            obj.wid = data.othis.parent().find("input").data('wcode');
        } else {
            obj.wid = "";
        }
        if (!!data.othis.parent().find("input").data('ecode') && data.othis.parent().find("input").data('ecode') != undefined && data.othis.parent().find("input").data('ecode') != "undefined") {
            obj.eid = data.othis.parent().find("input").data('ecode');
        } else {
            obj.eid = "";
        }
        var val = data.value;
        var title = data.elem.title;
        if (data.elem.checked == true) {
            // console.log("房间：" + val + "选中了");
            chooseIdArray.push(val);
            chooseItemArray.push(obj);

            if ($(".room-box-container").find("input[name='cb-room']").length == $(".room-box-container").find("input[name='cb-room']:checked").length) {
                $(".floor-box-container").find(".layui-this").find("input[name='cb-floor']").attr("checked", true);
                form.render();
                $(".floor-box-container").find(".layui-this").find(".layui-form-checkbox").addClass("layui-form-checked");
                chooseIdArray.push($(".floor-box-container").find(".layui-this").find("input[name='cb-floor']").val());
                window.event.stopPropagation();
            }
        } else if (data.elem.checked == false) {
            // console.log("房间：" + val + "取消选中了");
            chooseIdArray = $.grep(chooseIdArray, function (value) {
                return value != val;
            });
            for (var j = 0; j < chooseItemArray.length; j++) {
                if (chooseItemArray[j].id == obj.id) {
                    chooseItemArray.splice(j, 1);
                    j--;
                }
            }
            $(".floor-box-container").find(".layui-this").find("input[name='cb-floor']").removeAttr("checked");
            form.render('checkbox');
            chooseIdArray = $.grep(chooseIdArray, function (value) {
                return value != $(".floor-box-container").find(".layui-this").data('id');
            });
            window.event.stopPropagation();
        }
        change.itemSelectChange();
    });
    // $(".contract-cycle").html(today + " ~ " + today);   // 合同周期初始化赋值
    init.initUser();
    init.initProperty();
    init.initBusiness();
    init.initInputChange();
    // $(".layui-btn-disabled").removeClass("layui-btn-disabled"); // 没用---测试完成删除
});
/**
 * 初始化方法集合
 * 使用： init.function()
 * param: obj传this，调用该方法的对象
 *
 */
var init = {
    // 初始化当前登录用户
    initUser: function () {
        $.ajax({
            url: "/happuser/userInfo",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".user_name").html(res.data.name);
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化物业收费项
    initProperty: function () {
        $.ajax({
            url: "/dictionary/findByCode",
            data: {code: "PRO_002"},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    var data = res.data;
                    if (!!data) {
                        for (var i = 2; i < data.length; i++) {
                            var str = '<div><input type="checkbox" name="extra" lay-filter="extra" value="' + data[i].ID + '" title="' + data[i].NAME + '"></div>';
                            $(".layui-input-checkbox").append(str);
                        }
                    }
                    form.render();
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化房间下拉框商圈
    initBusiness: function () {
        $.ajax({
            url: "/business/list",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".business-box-container").empty();
                    if (res.data.length > 0) {
                        for (var i = 0; i < res.data.length; i++) {
                            if (res.data[i].IS_OPEN === "on") {
                                var str = '<dd data-id="' + res.data[i].ID + '" title="' + res.data[i].BUSINESS_NAME + '">' + res.data[i].BUSINESS_NAME + '</dd>';
                                $(".business-box-container").append(str);
                            }
                        }
                    }
                    $(".business-box-container dd").click(function (e) {
                        $(this).addClass('layui-this').siblings().removeClass("layui-this");
                        init.initBuilding($(this).data("id"));
                        $(".building-box-container").show();
                        $(".unit-box-container").hide();
                        $(".floor-box-container").hide();
                        $(".room-box-container").hide();
                    });
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化房间下拉框楼宇
    initBuilding: function (businessId) {
        $.ajax({
            url: "/building/list",
            data: {BUSINESS_ID: businessId},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".building-box-container").empty();
                    if (res.data.length > 0) {
                        for (var i = 0; i < res.data.length; i++) {
                            if (res.data[i].IS_OPEN === "on") {
                                var str = '<dd data-id="' + res.data[i].BUILDING_ID + '" title="' + res.data[i].BUILDING_NAME + '">' + res.data[i].BUILDING_NAME + '</dd>';
                                $(".building-box-container").append(str);
                            }
                        }
                    }
                    $(".building-box-container dd").click(function (e) {
                        $(this).addClass('layui-this').siblings().removeClass("layui-this");
                        init.initUnit($(this).data("id"));
                        $(".unit-box-container").show();
                        $(".floor-box-container").hide();
                        $(".room-box-container").hide();
                    });
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化房间下拉框单元
    initUnit: function (buildingId) {
        $.ajax({
            url: "/building/info",
            data: {BUILDING_ID: buildingId},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".unit-box-container").empty();
                    if (res.unitList.length > 0) {
                        for (var i = 0; i < res.unitList.length; i++) {
                            if (res.unitList[i].IS_OPEN === "on") {
                                var str = '<dd class="dd-unit" data-id="' + res.unitList[i].UNIT_ID + '" title="' + res.unitList[i].UNIT_NAME + ' 单元">' + res.unitList[i].UNIT_NAME + ' 单元</dd>';
                                $(".unit-box-container").append(str);
                            }
                        }
                    }
                    if (res.itemList.length > 0) {
                        for (var i = 0; i < res.itemList.length; i++) {
                            var str = '<dd class="dd-item" data-area="' + res.itemList[i].ROOM_AREA + '" data-id="' + res.itemList[i].ROOM_ID + '" title="【' + res.itemList[i].ROOM_TYPE_NAME + '】' + res.itemList[i].ROOM_NUM + '"><input type="checkbox" name="cb-item" value="' + res.itemList[i].ROOM_ID + '" data-name="' + res.itemList[i].ROOM_NUM + '" data-area="' + res.itemList[i].ROOM_AREA + '" data-wcode="' + res.itemList[i].WATER_CODE + '" data-ecode="' + res.itemList[i].ELECTRIC_CODE + '" lay-filter="cb-item" lay-skin="primary">【' + res.itemList[i].ROOM_TYPE_NAME + '】' + res.itemList[i].ROOM_NUM + '</dd>';
                            $(".unit-box-container").append(str);
                        }
                    }
                    form.render();
                    $(".unit-box-container dd.dd-unit").click(function (e) {
                        $(this).addClass('layui-this').siblings().removeClass("layui-this");
                        $(".floor-box-container").show();
                        $(".room-box-container").hide();
                        init.initFloor($(this).data("id"));
                    });
                    $(".unit-box-container dd.dd-item").click(function (e) {
                        $(this).siblings().removeClass("layui-this");
                        $(".floor-box-container").hide();
                        $(".room-box-container").hide();

                    });
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化房间下拉框楼层
    initFloor: function (unitId) {
        $.ajax({
            url: "/building/floorInfo",
            data: {UNIT_ID: unitId},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".floor-box-container").empty();
                    if (res.data.length > 0) {
                        for (var i = res.data.length - 1; i >= 0; i--) {
                            var chooseFlag = "";
                            $.each(chooseIdArray, function (index, value) {
                                if (res.data[i].FLOOR_ID == value) {
                                    chooseFlag = "checked";
                                }
                            });
                            if (res.data[i].IS_OPEN === "on") {
                                var str = '<dd data-id="' + res.data[i].FLOOR_ID + '" data-bid="' + res.data[i].BUILDING_ID + '" title="' + res.data[i].FLOOR_NAME + ' 层"><input type="checkbox" name="cb-floor" value="' + res.data[i].FLOOR_ID + '" lay-filter="cb-floor" lay-skin="primary" ' + chooseFlag + '> ' + res.data[i].FLOOR_NAME + ' 层</dd>';
                                $(".floor-box-container").append(str);
                            }
                        }
                    }
                    form.render();
                    $(".floor-box-container dd").click(function (e) {
                        $(this).addClass('layui-this').siblings().removeClass("layui-this");
                        init.initRoom($(this).data("bid"), $(this).data("id"));
                        $(".room-box-container").show();
                        e.stopPropagation();
                    });
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 初始化房间下拉框房间
    initRoom: function (buildingId, floorId) {
        $.ajax({
            url: "/room/list",
            data: {BUILDING_ID: buildingId, FLOOR_ID: floorId},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0") {
                    $(".room-box-container").empty();
                    var chooseFlag = "";
                    $.each(chooseIdArray, function (index, value) {
                        if (floorId == value) {
                            chooseFlag = "checked";
                        }
                    });
                    if (chooseFlag == "checked") {
                        for (var i = 0; i < res.data.length; i++) {
                            chooseIdArray = $.grep(chooseIdArray, function (value) {
                                return value != res.data[i].ROOM_ID;
                            });
                            for (var j = 0; j < chooseItemArray.length; j++) {
                                if (chooseItemArray[j].id == res.data[i].ROOM_ID) {
                                    chooseItemArray.splice(j, 1);
                                    j--;
                                }
                            }
                        }
                        for (var i = 0; i < res.data.length; i++) {
                            chooseIdArray.push(res.data[i].ROOM_ID);
                            var obj = {};
                            obj.id = res.data[i].ROOM_ID;
                            obj.name = res.data[i].ROOM_NUM;
                            obj.area = res.data[i].ROOM_AREA;
                            if (!!res.data[i].WATER_CODE && res.data[i].WATER_CODE != undefined && res.data[i].WATER_CODE != "undefined") {
                                obj.wid = res.data[i].WATER_CODE;
                            } else {
                                obj.wid = "";
                            }
                            if (!!res.data[i].ELECTRIC_CODE && res.data[i].ELECTRIC_CODE != undefined && res.data[i].ELECTRIC_CODE != "undefined") {
                                obj.eid = res.data[i].ELECTRIC_CODE;
                            } else {
                                obj.eid = "";
                            }
                            chooseItemArray.push(obj);
                        }
                    }
                    if (res.data.length > 0) {
                        for (var i = 0; i < res.data.length; i++) {
                            var chooseFlag1 = "";
                            $.each(chooseIdArray, function (index, value) {
                                if (res.data[i].ROOM_ID == value) {
                                    chooseFlag1 = "checked";
                                }
                            });
                            if (res.data[i].IS_OPEN === "on") {
                                var str = '<dd data-id="' + res.data[i].ROOM_ID + '" data-wcode="' + res.data[i].WATER_CODE + '" data-ecode="' + res.data[i].ELECTRIC_CODE + '" data-area="' + res.data[i].ROOM_AREA + '" title="' + res.data[i].ROOM_NAME + '"><input type="checkbox" name="cb-room" value="' + res.data[i].ROOM_ID + '" data-name="' + res.data[i].ROOM_NUM + '" data-area="' + res.data[i].ROOM_AREA + '" data-wcode="' + res.data[i].WATER_CODE + '" data-ecode="' + res.data[i].ELECTRIC_CODE + '" lay-filter="cb-room" lay-skin="primary" ' + chooseFlag1 + '> ' + res.data[i].ROOM_NUM + '</dd>';
                                $(".room-box-container").append(str);
                            }
                        }
                    }
                    form.render();
                    change.itemSelectChange();
                    // 已经选择的不可以再次选择
                    $("input[name='room']").each(function () {
                        var id = $(this).attr('data-id');
                        if (!!id) {
                            $(".room-box-container").find("dd").each(function () {
                                if ($(this).attr('data-id') == id) {
                                    $(this).remove();
                                }
                            });
                        }
                    });
                    $(".room-box-container dd").click(function (e) {

                    });
                } else if (res.errcode === "1") {
                    layer.msg(res.errmsg, function () {
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    // 所有的折扣的输入框绑定输入事件
    initInputChange: function () {
        $(".contract-discount-body").find("input").unbind("input");
        $(".contract-discount-body").find("input").on("input", function () {
            change.change($(this));
        });
    },

};
/**
 * 操作方法集合
 * 使用： operate.function()
 * param: obj传this，调用该方法的对象
 *
 */
var operate = {
    roomClick: function(obj,v) {
        chooseItemArray=new Array();//重新选择下一组
        chooseIdArray = new Array(); // 重新选择ID数组
        //alert(v)
        //alert(index_c)
    chooseOnOff = true;
        if(v!=index_c){
            index_c=v;
        }
    $(".room-select-box-container" +v+"").hide();
    $(".room-select-box-container-btn").hide();
    $(obj).parent().find(".room-select-box-container"+v+"").show();
    if (chooseItemArray.length > 0) {
        $(obj).parent().find(".room-select-box-container-btn").show();
    }
    init.initBusiness();
    window.event.stopPropagation();
},
    // 选择完房间项目确认
    chooseRoom: function () {
        var str = "";
        var strID = "";//保存id
        var allArea = 0;
        $.each(chooseItemArray, function (index, value) {
            // console.log(index, value);
            if (index == 0) {
                str += value.name;
            } else {
                str += "," + value.name;
            }
            allArea += Number(value.area);
        });
        $.each(chooseIdArray, function (index, value) {
            // console.log(index, value);
            if (index == 0) {
                strID += value;
            } else {
                strID += "," + value;
            }
        });
        //alert(strID)
        //$("input[name='room']").val(str);
        //alert(str)
        //alert(index_c)
        if("0"==index_c){
            $(".choose-room").val(str);//控制停车场id
        }else {
            $(".choose-room"+index_c+"").val(str);//控制房间id
        }
        //添加输入框的 房间id
        if("0"==index_c){
            $(".ROOMID").val(strID);//控制停车场
        }else {
            $(".ROOMID"+index_c+"").val(strID);//控制房间
        }
        $(".room-area-single").html(allArea);
    },
    // 克隆方法
    clone: function (obj) {
        var a = $(obj).parent().parent().find(".clone-box:last");
        var index = $(a).attr("data-index");
        var b = a.clone(true);
        $(b).attr("data-index", parseInt(index) + 1);
        $(b).find(".fa-plus-circle").hide();
        $(b).find(".fa-times-circle").show();

        $(b).find("input[name='room']").val("");
        $(b).find("input[name='room']").attr("data-id", "");
        $(b).find(".box-container dd").removeClass("layui-this");
        $(b).find(".building-box-container").empty();
        $(b).find(".building-box-container").hide();
        $(b).find(".unit-box-container").empty();
        $(b).find(".unit-box-container").hide();
        $(b).find(".floor-box-container").empty();
        $(b).find(".floor-box-container").hide();
        $(b).find(".room-box-container").empty();
        $(b).find(".room-box-container").hide();

        $(b).find(".room-area-single").empty();
        $(b).find(".room-area").hide();
        $(b).find("input[type='checkbox']").each(function () {
            $(this).prop("checked", false);
        });
        $(obj).parent().parent().append(b);
        form.render("select");
        $("input[name='securityDepositMoney']").val(calculate.securityDeposit().toFixed(2));
    },
    // 移除当前信息的方法，tier为几层父级
    remove: function (obj, tier) {
        switch (tier) {
            case 1:
                $(obj).parent().remove();
                break;
            case 2:
                $(obj).parent().parent().remove();
                break;
            case 3:
                $(obj).parent().parent().remove();
                break;
        }
    },
    // 增加账单
    addBill: function (obj,v) {
        //alert(v)
        index_bill++;
        //alert(index_bill)
        var str = '<div class="layui-field-box">\n' +
            '\n' +
            '                                <div class="layui-col-xs1">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '                                        <select name="PRICE_TYPE'+index_bill+'" lay-verify="required" lay-search="">\n' +
            '                                            <option value="0" selected="">收入</option>\n' +
            '                                            <option value="1">支出</option>\n' +
            '                                        </select>\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                                <div class="layui-col-xs4">\n' +
            '                                    <input type="number" class="layui-input" name="'+v+index_bill+'" id="test5" placeholder="">\n' +
            '                                    <span class="label-money">元</span>\n' +
            '                                </div>\n' +
            '                                <div class="layui-col-xs2">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '                                        <select name="ALLOT_BILL'+index_bill+'" lay-verify="required" lay-search="">\n' +
            '                                            <option value="0" selected="">公户</option>\n' +
            '                                            <option value="1">私户</option>\n' +
            '                                        </select>\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                                <div class="bill-add">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n' +
            '                                <span onclick="operate.remove(this,2)">\n' +
            '                                        <i class="fa fa-times-circle" aria-hidden="true"></i>\n' +
            '                                        <span>删除</span>\n' +
            '                                    </span>\n' +
            '                                </div>\n' +
            '                            </div>';
        $(obj).parent().parent().parent().find(".bill-arr").append(str);
        form.render('select');
    },
    // 增加 停车费
    addTCF: function (obj,v) {
        index_c++;
        var str = '<div class="layui-field-box">\n' +
            '\n' +
            '                                <div class="layui-col-xs1">\n' +
            '                                    <div class="">\n' +
           '停车费'+
            '                                    </div>\n' +
            '                                </div>\n' +
            '                        <div class="layui-col-xs2">\n' +
            '                                    <div class="layui-input-inline room-select2">\n' +
            '                                        <input type="text" name="room'+index_c+'" placeholder="请选择房源项目"\n' +
            '                                                class="layui-input choose-room choose-room'+index_c+'" onclick="operate.roomClick(this,' +index_c+');">\n' +
            '                                        <div>\n' +
            '                                        <input type="hidden" name="ROOMID'+index_c+'" placeholder=""\n' +
            '                                               class="layui-input ROOMID'+index_c+'" >\n' +
            '                                            <div id="room-select-box-container" class="room-select-box-container room-select-box-container'+index_c+'  room-select-box-container2">\n' +
            '                                                <div class="displayflex">\n' +
            '                                                    <dl class="box-container business-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container building-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container unit-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container floor-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                    <dl class="box-container room-box-container">\n' +
            '                                                    </dl>\n' +
            '                                                </div>\n' +
            '                                            </div>\n' +
            '                                        </div>\n' +
            '                                    </div>\n' +
            '                                </div>'+
            '                  <div class="MKXFQH'+index_c+'">\n' +
            '                                <div class="layui-col-xs3">\n' +
            '                                    <input type="number" class="layui-input" name="ROOM_MONEY'+index_c+'" id="test5" placeholder="">\n' +
            '                                    <span class="label-money">元</span>\n' +
            '                                </div>\n' +

            '               <div class="layui-col-xs1">\n' +
            '                                        <input type="number" class="layui-input" name="PARK_CARD'+index_c+'" id="PARK_CARD" placeholder="">\n' +
            '                                        <span class="label-card">张</span>\n' +
            '                                </div>'+
            '                                <div class="layui-col-xs2">\n' +
            '                                    <div class="layui-input-inline">\n' +
            '                                        <select name="ALLOT_BILL'+index_c+'" lay-verify="required" lay-search="">\n' +
            '                                            <option value="0" selected="">公户</option>\n' +
            '                                            <option value="1">私户</option>\n' +
            '                                        </select>\n' +
            '                                    </div>\n' +
            '                                </div>\n' +
            '                             </div>\n'+
            '                                <div class="bill-add">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n' +
            '                                <span onclick="operate.remove(this,2)">\n' +
            '                                        <i class="fa fa-times-circle" aria-hidden="true"></i>\n' +
            '                                        <span>删除</span>\n' +
            '                                    </span>\n' +
            '                                </div>\n' +
            '                            </div>';
        $(obj).parent().parent().parent().find(".bill-arr").append(str);
        form.render('select')
    },
    // 移除当前信息的方法，tier为几层父级
    remove: function (obj, tier) {
        switch (tier) {
            case 1:
                $(obj).parent().remove();
                break;
            case 2:
                $(obj).parent().parent().remove();
                break;
            case 3:
                $(obj).parent().parent().remove();
                break;
        }
    }
};
/**
 * 改变方法集合
 * 使用： change.function()
 * param: obj传this，调用该方法的对象
 *
 */
var change = {
    // 房间项目下拉选择框复选框
    itemSelectChange: function () {
        if (chooseItemArray.length > 0) {
            $(".room-select-box-container-btn").show();
        } else {
            $(".room-select-box-container-btn").hide();
        }
    },
    // 有输入值改变
    change: function (obj) {
        // 如果状态为true的话显示小提示
        if (tips === true) {
            $(".quote-rent-detail").find(".quote-title").find(".layui-layer-content").fadeIn(200);
        }
        // 只有当data-type是unit-div里的输入框值改变的时候才去调用重新计算的方法
        if ($(obj).parent().parent().attr("data-type") === "unit-div") {
            change.rentOrdiscountOrIncreasingChange($(obj));
        }
        if ($(obj).attr("name") === "unit-discount") {
            change.rentOrdiscountOrIncreasingChange($(obj));
        }
    },

};
/**
 * 计算方法集合
 * 使用： calculate.function()
 * param: obj传this，调用该方法的对象
 *
 */
var calculate = {
    // 返回总面积
    totalArea: function () {
        var totalArea = 0;
        $(".room-area-single").each(function () {
            totalArea += Number($(this).html());
        });
        $(".totalArea").html(totalArea.toFixed(2));
        return totalArea;
    }

};







